package cn.vihub.service.impl;

import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.vihub.handler.MyRuntimeException;
import cn.vihub.param.ShopMenuParam;
import cn.vihub.pojo.ShopRoleToMenu;
import cn.vihub.pojo.ShopUserToRole;
import cn.vihub.service.ShopRoleToMenuService;
import cn.vihub.service.ShopUserToRoleService;
import cn.vihub.utils.Result;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.vihub.mapper.ShopMenuMapper;
import cn.vihub.pojo.ShopMenu;
import cn.vihub.service.ShopMenuService;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author: 秋晚
 * @Date:  2025/7/18 16:14
 * @Description: 
 **/

@Service
@Transactional(rollbackFor = Exception.class)
public class ShopMenuServiceImpl extends ServiceImpl<ShopMenuMapper, ShopMenu> implements ShopMenuService{

    @Autowired
    ShopUserToRoleService shopUserToRoleService;

    @Autowired
    ShopRoleToMenuService shopRoleToMenuService;

    @Autowired
    ShopMenuService shopMenuService;


    @Override
    public Boolean saveMenu(ShopMenuParam shopMenuParam) {

        if(ObjectUtil.isEmpty(shopMenuParam.getId())){
            shopMenuParam.setCreateTime(new Date());
        }else{
            shopMenuParam.setUpdateTime(new Date());
        }
        //创建一个新的菜单对象
        ShopMenu shopMenu = new ShopMenu();
        //拷贝
        BeanUtils.copyProperties(shopMenuParam,shopMenu);
        //返回
        return saveOrUpdate(shopMenu);
    }

    @Override
    public Object getTree() {
        //查询所有
        List<ShopMenu> list = list();

        //使用hutool工具类进行循环递归菜单树
        return TreeUtil.build(list,"0",(shopMenu , tree)->{
            tree.setId(shopMenu.getId()+"");
            tree.setParentId(shopMenu.getPid()+"");
            tree.setName(shopMenu.getName());
            tree.putExtra("typeId",shopMenu.getTypeId());
            tree.putExtra("uri",shopMenu.getUri());
            tree.putExtra("code",shopMenu.getCode());
        });
    }

    @Override
    public void isChildrenByPid(Integer id) {
        //查询数量
        int count = count(new LambdaQueryWrapper<ShopMenu>().eq(ShopMenu::getPid, id));
        if(count>0){
            //存在子菜单抛出自定义异常
            throw new MyRuntimeException("该菜单下存在子菜单禁止删除!");
        }
    }

    @Override
    public Object getTreeByUserId(Integer userId) {
        //根据userId查询出所有角色
        List<ShopUserToRole> roleByUsers = shopUserToRoleService.getRoleByUserId(userId);
        List<Integer> roleIds = roleByUsers.stream().map(ShopUserToRole::getRoleId).collect(Collectors.toList());

        //根据roleIds查询出权限
        List<ShopRoleToMenu> shopRoleToMenus=shopRoleToMenuService.isMenuByRoleIds(roleIds);
        //如果是空  就是用户未绑定角色
        if(shopRoleToMenus.size()==0){
            //抛出自定义异常
            throw new MyRuntimeException("该用户未绑定角色!");
        }
        //根据menuIds查询权限
        List<Integer> menuIds = shopRoleToMenus.stream().map(ShopRoleToMenu::getMenuId).collect(Collectors.toList());

        List<ShopMenu> shopMenus = listByIds(menuIds);

        //组装树状结构
        return TreeUtil.build(shopMenus,"0",(menu,tree)->{
            tree.setName(menu.getName());
            tree.setId(menu.getId()+"");
            tree.setParentId(menu.getPid()+"");
            tree.putExtra("uri",menu.getUri());
            tree.putExtra("typeId",menu.getTypeId());
        });
    }

    @Override
    public List<ShopMenu> getMenuById(Integer roleId) {
        List<ShopRoleToMenu> shopRoleToMenus = shopRoleToMenuService.list(new LambdaQueryWrapper<ShopRoleToMenu>().eq(ShopRoleToMenu::getRoleId, roleId));

        List<Integer> menuIds = shopRoleToMenus.stream().map(ShopRoleToMenu::getMenuId).collect(Collectors.toList());

        return listByIds(menuIds);
    }
}
